﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Http;
using System.Net.Http.Headers;
using XclusiveClient.Classes;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace XclusiveClient
{
    public partial class ViewAccount : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri("http://localhost:3676/");
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage response = client.GetAsync("api/Client/?company_name=" + Session["company_name"]).Result;
            if (response.IsSuccessStatusCode)
            {
                var c = response.Content.ReadAsAsync<Client>().Result;

                lblusername.Text = c.username.ToString();
                lblfull_name.Text = c.full_name.ToString();
                lblemail.Text = c.email.ToString();
                lbladdress.Text = c.address.ToString();
                lblcontact_number.Text = c.contact_number.ToString();
                lblcompany_country.Text = c.company_country.ToString();
                lblcompany_name.Text = c.company_name.ToString();
                lblcompany_email.Text = c.company_email.ToString();
                lblcompany_address.Text = c.company_address.ToString();
            }
            else
            {
                lblStatus.Text = "Could not register for an account. Error code: " + response.StatusCode + ", Reason: " + response.ReasonPhrase.ToString() + "<br/>";
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri("http://localhost:3676/");
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage response = client.DeleteAsync("api/Client/?company_name=" + Session["company_name"]).Result;
            if (response.IsSuccessStatusCode)
            {
                lblusername.Text = null;
                lblfull_name.Text = null;
                lblemail.Text = null;
                lbladdress.Text = null;
                lblcontact_number.Text = null;
                lblcompany_country.Text = null;
                lblcompany_name.Text = null;
                lblcompany_email.Text = null;
                lblcompany_address.Text = null;
                lblStatus.Visible = true;

                lblStatus.Text = "Acount Deleted...";
                Session.RemoveAll();
                Session.Abandon();
                Response.Redirect("~/Default.aspx");
            }
            else
            {
                lblStatus.Visible = true;
                lblStatus.Text = "Could not delete account. Error code: " + response.StatusCode + ", Reason: " + response.ReasonPhrase.ToString() + "<br/>";
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            Session["company_name"] = lblcompany_name.Text;
            Response.Redirect("~/XLAccount/EditAccount.aspx");
        }

        protected void btnLogout_Click(object sender, EventArgs e)
        {
            Session.RemoveAll();
            Session.Abandon();
            Response.Redirect("~/Default.aspx");
        }
    }
}